package io.aircraft.modules.wms.controller;

import io.aircraft.common.annotation.LogOperation;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.common.utils.Result;
import io.aircraft.common.validator.AssertUtils;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.common.validator.group.AddGroup;
import io.aircraft.common.validator.group.DefaultGroup;
import io.aircraft.common.validator.group.UpdateGroup;
import io.aircraft.modules.security.user.SecurityUser;
import io.aircraft.modules.security.user.UserDetail;
import io.aircraft.modules.wms.dto.TrkPostDTO;
import io.aircraft.modules.wms.entity.TrkPostEntity;
import io.aircraft.modules.wms.service.TrkPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 设备任务
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-21
 */
@RestController
@RequestMapping("wms/trk-post")
public class TrkPostController {
    @Autowired
    private TrkPostService trkPostService;

    @GetMapping("page")
    @RequiresPermissions("wms:trk-post:page")
    public Result<PageData<TrkPostDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<TrkPostDTO> page = trkPostService.page(params);

        return new Result<PageData<TrkPostDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:trk-post:info")
    public Result<TrkPostDTO> get(@PathVariable("id") Long id) {
        TrkPostDTO data = trkPostService.get(id);

        return new Result<TrkPostDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:trk-post:save")
    public Result save(@RequestBody TrkPostDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        trkPostService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:trk-post:update")
    public Result update(@RequestBody TrkPostDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        trkPostService.update(dto);

        return new Result();
    }

    @PostMapping("/simulate/{trkId}")
    @LogOperation("模拟完成")
    @RequiresPermissions("wms:trk-post:update")
    public Result simulate(@PathVariable("trkId") Long trkId) {
        //获取操作用户
        UserDetail user = SecurityUser.getUser();

        TrkPostEntity entity = trkPostService.selectById(trkId);
        //TODO 调用模拟完成存储过程

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:trk-post:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        trkPostService.delete(ids);

        return new Result();
    }
}